/*
 * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.bjmashibing.system.huawei.huawei_push.push.examples;

import com.alibaba.fastjson.JSONObject;
import com.bjmashibing.system.huawei.static_var;
import com.bjmashibing.system.huawei.huawei_push.push.android.AndroidNotification;
import com.bjmashibing.system.huawei.huawei_push.push.android.BadgeNotification;
import com.bjmashibing.system.huawei.huawei_push.push.android.Button;
import com.bjmashibing.system.huawei.huawei_push.push.android.ClickAction;
import com.bjmashibing.system.huawei.huawei_push.push.android.Color;
import com.bjmashibing.system.huawei.huawei_push.push.android.LightSettings;
import com.bjmashibing.system.huawei.huawei_push.push.exception.HuaweiMesssagingException;
import com.bjmashibing.system.huawei.huawei_push.push.message.AndroidConfig;
import com.bjmashibing.system.huawei.huawei_push.push.message.Message;
import com.bjmashibing.system.huawei.huawei_push.push.message.Notification;
import com.bjmashibing.system.huawei.huawei_push.push.messaging.HuaweiApp;
import com.bjmashibing.system.huawei.huawei_push.push.messaging.HuaweiMessaging;
import com.bjmashibing.system.huawei.huawei_push.push.model.Urgency;
import com.bjmashibing.system.huawei.huawei_push.push.model.Importance;
import com.bjmashibing.system.huawei.huawei_push.push.model.Visibility;
import com.bjmashibing.system.huawei.huawei_push.push.reponse.SendResponse;
import com.bjmashibing.system.huawei.huawei_push.push.util.InitAppUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SendNotifyMessage {
    private final static Logger logger = LoggerFactory.getLogger(SendNotifyMessage.class);
    /**
     * send notification message
     *
     * @throws HuaweiMesssagingException
     * pushscheme://com.huawei.codelabpush/deeplink?name=wangyuhong&age=33
     */
    public void sendNotification() throws HuaweiMesssagingException {
        HuaweiApp app = InitAppUtils.initializeApp();
        HuaweiMessaging huaweiMessaging = HuaweiMessaging.getInstance(app);

        Notification notification = Notification.builder().setTitle("sample title")
                .setBody("sample message body")
                .build();

        JSONObject multiLangKey = new JSONObject();
        JSONObject titleKey = new JSONObject();
        titleKey.put("en","好友请求");
        JSONObject bodyKey = new JSONObject();
        titleKey.put("en","My name is %s, I am from %s.");
        multiLangKey.put("key1", titleKey);
        multiLangKey.put("key2", bodyKey);

        LightSettings lightSettings = LightSettings.builder().setColor(Color.builder().setAlpha(0f).setRed(0f).setBlue(1f).setGreen(1f).build())
                .setLightOnDuration("3.5")
                .setLightOffDuration("5S")
                .build();


        JSONObject bbb = new JSONObject();

        JSONObject aaa = new JSONObject();
        aaa.put("name", "wangyuhong");
        aaa.put("age", "22");
        bbb.put("data", aaa);

        AndroidNotification androidNotification = AndroidNotification.builder().setIcon("/raw/ic_launcher2")

                .setColor("#AACCDD")
                .setSound("/raw/shake")
                .setDefaultSound(true)
                .setTag("tagBoom")
//                .setClickAction(ClickAction.builder().setType(2).setUrl("https://www.huawei.com").build())
//                .setClickAction(ClickAction.builder().setType(1).setIntent("pushscheme://com.huawei.codelabpush/deeplink?name=wangyuhong&age=33").build())
//                .setClickAction(ClickAction.builder().setType(1).setIntent("android.intent.action.MAIN").build())
                .setClickAction(ClickAction.builder().setType(3).build())
                .setBodyLocKey("M.String.body")
                .addBodyLocArgs("boy").addBodyLocArgs("dog")
                .setTitleLocKey("M.String.title")
                .addTitleLocArgs("Girl").addTitleLocArgs("Cat")
                .setChannelId("Your Channel ID")
                .setNotifySummary("some summary")
                .setMultiLangkey(multiLangKey)
                .setStyle(1)
                .setBigTitle("Big Boom Title")
                .setBigBody("Big Boom Body")
                .setAutoClear(86400000)
                .setNotifyId(486)
                .setGroup("Group1")
                .setImportance(Importance.LOW.getValue())
                .setLightSettings(lightSettings)
                .setBadge(BadgeNotification.builder().setAddNum(1).setBadgeClass("Classic").build())
                .setVisibility(Visibility.PUBLIC.getValue())
                .setForegroundShow(true)
                .addInboxContent("content1").addInboxContent("content2").addInboxContent("content3").addInboxContent("content4").addInboxContent("content5")
//                .addButton(Button.builder().setName("button1").setActionType(0).build())
                .addButton(Button.builder().setName("app首页").setActionType(0).setData(aaa.toString()).setIntent("android.intent.action.MAIN").build())
//                .addButton(Button.builder().setName("button2").setActionType(1).setIntentType(0).setIntent("https://com.huawei.hms.hmsdemo/deeplink").build())
//                .addButton(Button.builder().setName("button2").setActionType(1).setIntentType(0).setIntent("pushscheme://com.huawei.codelabpush/deeplink?name=wangyuhong&age=33").build())
                .addButton(Button.builder().setName("Activity1").setActionType(1).setIntentType(1).setData(aaa.toString()).setIntent("com.huawei.codelabpush.intent.action.test").build())
//                .addButton(Button.builder().setName("清除通知").setActionType(3).setData("your share link").build())
//                .addButton(Button.builder().setName("分享功能").setActionType(4).setData("your share link").build())
                .build();

        AndroidConfig androidConfig = AndroidConfig.builder().setCollapseKey(-1)
                .setUrgency(Urgency.HIGH.getValue())
                .setTtl("10000s")
                .setBiTag("the_sample_bi_tag_for_receipt_service")
                .setNotification(androidNotification)
                .build();

        Message message = Message.builder().setNotification(notification)
                .setAndroidConfig(androidConfig)
                .setData(aaa.toString())
//                .addToken("AND8rUp4etqJvbakK7qQoCVgFHnROXzH8o7B8fTl9rMP5VRFN83zU3Nvmabm3xw7e3gZjyBbp_wfO1jP-UyDQcZN_CtjBpoa7nx1WaVFe_3mqXMJ6nXJNUZcDyO_-k3sSw")
                .addToken("ANCNwOuV2L89-qG0UO2gtw-OCaAUBAy7PgE7YXM2dPPMgSKTwbg2Ogs6_NlvLKhbmCCVSo8RUmwAkgYpf--I_MbBSDCqE3U1xVtUWqJa9qD9u12EFrhXjXpxiqPcs35qgg")
                .build();

        SendResponse response = huaweiMessaging.sendMessage(message);
    }

    public void huawei_initialize_app(){
        static_var.huawei_app = InitAppUtils.initializeApp();
    }

    public void sendNotification2(String str_token,String str_title,String str_big_body,String str_machine_account,String str_event_info) throws HuaweiMesssagingException {
        try{
            HuaweiMessaging huaweiMessaging = HuaweiMessaging.getInstance(static_var.huawei_app);

//        Notification notification = Notification.builder().setTitle("sample title")
            Notification notification = Notification.builder().setTitle(str_title)
                    .setBody("sample message body")
                    .build();

            JSONObject multiLangKey = new JSONObject();
            JSONObject titleKey = new JSONObject();
            titleKey.put("en","好友请求");
            JSONObject bodyKey = new JSONObject();
            titleKey.put("en","My name is %s, I am from %s.");
            multiLangKey.put("key1", titleKey);
            multiLangKey.put("key2", bodyKey);

            LightSettings lightSettings = LightSettings.builder().setColor(Color.builder().setAlpha(0f).setRed(0f).setBlue(1f).setGreen(1f).build())
                    .setLightOnDuration("3.5")
                    .setLightOffDuration("5S")
                    .build();


            JSONObject bbb = new JSONObject();

            JSONObject aaa = new JSONObject();
            aaa.put("name", "wangyuhong");
            aaa.put("age", "22");
            aaa.put("machine_account", str_machine_account);
            aaa.put("event_info", str_event_info);
            aaa.put("machine_info", str_big_body);
            bbb.put("data", aaa);

            AndroidNotification androidNotification = AndroidNotification.builder().setIcon("/raw/ic_launcher2")

                    .setColor("#AACCDD")
                    .setSound("/raw/shake")
                    .setDefaultSound(true)
                    .setTag("tagBoom")
//                .setClickAction(ClickAction.builder().setType(2).setUrl("https://www.huawei.com").build())
//                .setClickAction(ClickAction.builder().setType(1).setIntent("pushscheme://com.huawei.codelabpush/deeplink?name=wangyuhong&age=33").build())
//                .setClickAction(ClickAction.builder().setType(1).setIntent("android.intent.action.MAIN").build())
                    .setClickAction(ClickAction.builder().setType(3).build())
                    .setBodyLocKey("M.String.body")
                    .addBodyLocArgs("boy").addBodyLocArgs("dog")
                    .setTitleLocKey("M.String.title")
                    .addTitleLocArgs("Girl").addTitleLocArgs("Cat")
                    .setChannelId("RingRing")
                    .setImportance(Importance.HIGH.getValue())

                    .setNotifySummary("HTZSAFE")
                    .setMultiLangkey(multiLangKey)
                    .setStyle(1)
                    .setBigTitle("Big Boom Title")
//                .setBigBody("Big Boom Body")
                    .setBigBody(str_big_body)
                    .setAutoClear(86400000)
                    .setNotifyId(486)
                    .setGroup("Group1")
                    .setImportance(Importance.LOW.getValue())
                    .setLightSettings(lightSettings)
                    .setBadge(BadgeNotification.builder().setAddNum(1).setBadgeClass("Classic").build())
                    .setVisibility(Visibility.PUBLIC.getValue())
                    .setForegroundShow(true)
                    .addInboxContent("content1").addInboxContent("content2").addInboxContent("content3").addInboxContent("content4").addInboxContent("content5")
//                .addButton(Button.builder().setName("button1").setActionType(0).build())
                    .addButton(Button.builder().setName("app首页").setActionType(0).setData(aaa.toString()).setIntent("android.intent.action.MAIN").build())
//                .addButton(Button.builder().setName("button2").setActionType(1).setIntentType(0).setIntent("https://com.huawei.hms.hmsdemo/deeplink").build())
//                .addButton(Button.builder().setName("button2").setActionType(1).setIntentType(0).setIntent("pushscheme://com.huawei.codelabpush/deeplink?name=wangyuhong&age=33").build())
//                .addButton(Button.builder().setName("Activity1").setActionType(1).setIntentType(1).setData(aaa.toString()).setIntent("com.huawei.codelabpush.intent.action.test").build())
//                .addButton(Button.builder().setName("清除通知").setActionType(3).setData("your share link").build())
//                .addButton(Button.builder().setName("分享功能").setActionType(4).setData("your share link").build())
                    .build();

            AndroidConfig androidConfig = AndroidConfig.builder().setCollapseKey(-1)
                    .setUrgency(Urgency.HIGH.getValue())
                    .setTtl("10000s")
                    .setBiTag("hengbo_receipt_service7997")
                    .setNotification(androidNotification)
                    .build();

            Message message = Message.builder().setNotification(notification)
                    .setAndroidConfig(androidConfig)
                    .setData(aaa.toString())
//                .addToken("AND8rUp4etqJvbakK7qQoCVgFHnROXzH8o7B8fTl9rMP5VRFN83zU3Nvmabm3xw7e3gZjyBbp_wfO1jP-UyDQcZN_CtjBpoa7nx1WaVFe_3mqXMJ6nXJNUZcDyO_-k3sSw")
//                .addToken("ALG_b544ZNzPk-DJPiq4vZkusxsHhYPiVVoZXjvtQLTCDMoV3E0tfUyqW4mZgb51vpDwzI0ZAzchwy-jARxgw3FwE6tAZnwbZo5-Da8BvRcndX14Y5sMiW-iaeXlzuebOg")
//                .addToken("AC5ZGZKRUP7217clxx8PTa19H5lzhyCmnJCdNs60rCLDWF5ZYNnjkvd7Frj0bGoFW_cYYL_7vBdZJwEtMjHeXBYbfRjguOjdvVqfkICdMKYrgDq36ytiBDvOfdFgwmciqg")
                    .addToken(str_token)
                    .build();

            SendResponse response = huaweiMessaging.sendMessage(message);
            String code = response.getCode();
            String msg = response.getMsg();

//        logger.info("huawei_push,code=" + code);
            logger.info("huawei_push,msg=" + msg + "," + str_machine_account);
        }
        catch(Exception e){
            logger.info("sendNotification2,error=",e);
            e.printStackTrace();
        }

    }
}
